package com.douglas.managesystem.service.system.impl;

import com.douglas.managesystem.common.SearchEntity;
import com.douglas.managesystem.common.TreeUtils;
import com.douglas.managesystem.dao.UserMapper;
import com.douglas.managesystem.dao.system.MenuMapper;
import com.douglas.managesystem.entity.User;
import com.douglas.managesystem.entity.system.Menu;
import com.douglas.managesystem.service.system.MenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description 菜单业务层
 * @Author 以梦为馬 <Douglas1314@126.com>
 * @Version V1.0.0
 * @Date 2020-11-13 23:49
 **/
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 1.根据用户id查询用户拥有的菜单权限
     * @param userId
     * @return
     */
    @Override
    public List<Menu> selectMenuByUserId(String userId) {
        List<Menu> menus = menuMapper.findByBelong(userId);
        return TreeUtils.getChildPerms(menus, 0);
    }

    /**
     * 2.查询所有可以使用的菜单
     * @return
     */
    @Override
    public PageInfo<Menu> selectAllMenuByPage(SearchEntity params) {
        if(params.getPageNum() == null || params.getPageNum() == 0) {
            params.setPageNum(1);
        }
        if(params.getPageSize() == null || params.getPageSize() == 0) {
            params.setPageSize(10);
        }
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Menu> list = menuMapper.queryListByPage(params.getEntity());
        PageInfo<Menu> page = new PageInfo<Menu>(list);
        return page;
    }

    /**
     * 3.创建菜单
     * @param menu
     */
    @Override
    public void addMenu(Menu menu) {
        menuMapper.insertMenu(menu);
    }

    /**
     * 4.更新菜单
     * @param menu
     */
    @Override
    public void editMenu(Menu menu) {
        menuMapper.updateMenu(menu);
    }

    /**
     * 5.根据id查询菜单详情
     * @param id
     * @return
     */
    @Override
    public Menu selectMenuById(String id) {
        return menuMapper.queryMenuById(id);
    }

    /**
     * 6.根据id删除菜单 逻辑删除
     * @param id
     */
    @Override
    public void delete(String id) {
        menuMapper.delete(id);
    }
}
